<template>
  <div class="team-manager-div">
    <template v-if="!isCreatedReport">
      <a-card :bordered="false" :loading="loading">
        <a-empty :description="false" :image-style="{ height: '500px' }">
          <img slot="image" src="~@/assets/report-empty.png" alt="暂无报告" />
          <a-button type="primary" @click="createFinalReport" :loading="createLoading"> 生成总结报告 </a-button>
        </a-empty>
      </a-card>
    </template>

    <template v-else>
      <!--<img src=""/>-->

      <a-card title="" :bordered="false">
        <a-page-header style="border-bottom: 1px solid rgb(235, 237, 240)">
          <template slot="title">
            <h1>{{ report.reportName }}</h1>
            <span :style="{ marginRight: '10px', fontWeight: 'normal', fontSize: '16px' }"
              >报告时间：{{ report.reportCreateTime }}</span
            >
          </template>
          <template slot="subTitle">
            <a-badge :status="report.publishStatus == 0 ? 'warning' : 'success'" />
            {{ report.publishStatusText }}
          </template>
          <template slot="extra">
            <a-popconfirm placement="topLeft" ok-text="确定" cancel-text="取消" @confirm="removeFinalReport">
              <template slot="title">
                <p>你确定要删除这篇报告吗？</p>
              </template>
              <a-button style="margin-left: 30px" :loading="deleteLoading">删除报告</a-button>
            </a-popconfirm>
            <a-button key="打印报告" type="primary" @click="printReport"> 打印报告 </a-button>
            <!-- <a-button key="数据录入" type="primary" @click="physicalRecord"> 数据录入 {{ msg }} </a-button> -->
          </template>
        </a-page-header>
        <div :style="{ marginTop: '15px' }" ref="printReport">
          <basic-info :report="report.reportContent" v-if="report.reportContent.basicInfo"></basic-info>
          <service-programme
            :report="report.reportContent.serviceProgramme"
            v-if="report.reportContent.serviceProgramme"
          ></service-programme>
          <signin-record
            :report="report.reportContent.signinSummary"
            v-if="report.reportContent.signinSummary"
          ></signin-record>
          <target :report="report.reportContent.targets" v-if="report.reportContent.targets"></target>
          <punch-rate-compliance-rate
            :report="report.reportContent.signinAndReachStandardRate"
            v-if="report.reportContent.signinAndReachStandardRate"
          ></punch-rate-compliance-rate>
          <physical
            :archiveFirst="report.reportContent.archiveFirst.physicalReport"
            :archiveSummary="report.reportContent.archiveSummary.physicalReport"
            v-if="
              report.reportContent.archiveFirst.physicalReport || report.reportContent.archiveSummary.physicalReport
            "
            @fatherFn="physicalRecord"
          ></physical>
          <laboratory
            :archiveFirst="report.reportContent.archiveFirst.laboratoryReport"
            :archiveSummary="report.reportContent.archiveSummary.laboratoryReport"
            v-if="
              report.reportContent.archiveFirst.laboratoryReport || report.reportContent.archiveSummary.laboratoryReport
            "
            @fatherFn="laboratoryRecord"
          ></laboratory>
          <iconography
            :archiveFirst="report.reportContent.archiveFirst.iconographyReport"
            :archiveSummary="report.reportContent.archiveSummary.iconographyReport"
            v-if="
              report.reportContent.archiveFirst.iconographyReport ||
                report.reportContent.archiveSummary.iconographyReport
            "
            @fatherFn="icongraphyRecord"
          ></iconography>
          <evaluation
            :evaluationFirst="report.reportContent.evaluationFirst"
            :evaluationSummary="report.reportContent.evaluationSummary"
            v-if="report.reportContent.evaluationFirst || report.reportContent.evaluationSummary"
          ></evaluation>
          <suggests
            :summary="report.summary"
            @editSummary="
              val => {
                report.summary = val
              }
            "
          ></suggests>
        </div>
        <print-report></print-report>
        <a-row>
          <a-col :span="4" :offset="16">
            <div :style="{ fontSize: '16px', margin: '25px 0 5px 0', fontWeight: 600 }">
              <span style="">制表人：{{ report.creator }}</span>
            </div>
          </a-col>
          <a-col :span="4">
            <div :style="{ fontSize: '16px', margin: '25px 0 5px 0', fontWeight: 600 }">
              <span>日期：{{ report.reportCreateTime }}</span>
            </div>
          </a-col>
        </a-row>
        <a-row v-if="report.publishStatus == 0">
          <a-col :span="4" :offset="20">
            <a-button :style="{ margin: '15px 0' }" type="primary" :loading="publishLoading" @click="publishReport(1)"
              >发布</a-button
            >
          </a-col>
        </a-row>
        <a-row v-if="report.publishStatus == 1">
          <a-col :span="4" :offset="20">
            <a-button :style="{ margin: '15px 0' }" type="primary" :loading="publishLoading" @click="publishReport(0)"
              >取消发布</a-button
            >
          </a-col>
        </a-row>
      </a-card>
    </template>
  </div>
</template>

<script>
import { PAGE_MENU_TYPE } from '@/store/mutation-types'
import { getAction, postAction, deleteAction } from '@/api/manage'
import BasicInfo from './modules/First/BasicInfo.vue'
import ServiceProgramme from './modules/First/ServiceProgramme.vue'
import SigninRecord from './modules/Final/SigninRecord.vue'
import Target from './modules/Final/Target.vue'
import PunchRateComplianceRate from './modules/Final/PunchRateComplianceRate.vue'
import Physical from './modules/Final/Physical.vue'
import Laboratory from './modules/Final/Laboratory.vue'
import Iconography from './modules/Final/Iconography.vue'
import Evaluation from './modules/Final/Evaluation.vue'
import Suggests from './modules/Final/Suggests.vue'
import PrintReport from './modules/PrintReport/PrintReport.vue'
import print from '@/utils/print'
import bus from '@/utils/bus'

export default {
  name: 'Final',
  components: {
    BasicInfo,
    ServiceProgramme,
    SigninRecord,
    Target,
    PunchRateComplianceRate,
    Physical,
    Laboratory,
    Iconography,
    Evaluation,
    Suggests,
    PrintReport
  },
  data() {
    return {
      msg: '',
      loading: true,
      createLoading: false,
      deleteLoading: false,
      publishLoading: false,
      url: {
        getFinalReport: 'patient/report/summary',
        createFinalReport: 'patient/report/create',
        removeFinalReport: 'patient/report/remove',
        publishReport: 'patient/report/publish'
      },
      isCreatedReport: false,
      report: {
        createTime: '2022-3-30 15:14:32'
      }
    }
  },
  created() {
    this.getMyFinalReport()
    this.getModifiedDataForFinalReport()
  },
  mounted() {
    // this.getModifiedDataForFinalReport()
  },
  destroyed() {
    bus.$off()
  },
  methods: {
    getModifiedDataForFinalReport() {
      bus.$on('modifiedDataForFinalReportByPhysical', data => {
        const info = data.data
        const params = {
          archiveType: 1,
          status: '0',
          reportId: this.report.dataId
        }
        postAction('/patient/report/summary/update', params).then(res => {
          if (res.success) {
            this.getMyFinalReport()
          } else {
            console.log(res.message)
          }
        })
        console.log('modifiedDataForFinalReportByPhysical', data)
      })
      bus.$on('modifiedDataForFinalReportByLab', data => {
        const info = data.data
        const params = {
          archiveType: 2,
          status: '0',
          reportId: this.report.dataId
        }
        postAction('/patient/report/summary/update', params).then(res => {
          if (res.success) {
            this.getMyFinalReport()
          } else {
            console.log(res.message)
          }
        })
        console.log('modifiedDataForFinalReportByLab', data)
      })
      bus.$on('modifiedDataForFinalReportByIco', data => {
        const info = data.data
        const params = {
          archiveType: 3,
          status: '0',
          reportId: this.report.dataId
        }
        postAction('/patient/report/summary/update', params).then(res => {
          if (res.success) {
            this.getMyFinalReport()
          } else {
            console.log(res.message)
          }
        })
        console.log('modifiedDataForFinalReportByIco', data)
      })
    },
    printReport() {
      print(this.$refs.printReport)
    },
    getMyFinalReport() {
      getAction(this.url.getFinalReport, {
        patientId: this.$route.query[PAGE_MENU_TYPE.PatientId]
      })
        .then(res => {
          if (res.result != null && res.result.dataId) {
            this.isCreatedReport = true
            this.report = res.result
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    createFinalReport() {
      this.createLoading = true
      postAction(this.url.createFinalReport, {
        patientId: this.$route.query[PAGE_MENU_TYPE.PatientId],
        type: 4 // 1-首诊报告，4-总结报告
      })
        .then(res => {
          if (res.success) {
            this.$message.success('总结报告创建成功')
            this.getMyFinalReport()
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(_ => {
          this.createLoading = false
        })
    },
    removeFinalReport() {
      if (this.report.dataId) {
        this.deleteLoading = true
        deleteAction(this.url.removeFinalReport, {
          id: this.report.dataId
        })
          .then(res => {
            if (res.success) {
              this.$message.success('总结报告删除成功')
              this.isCreatedReport = false
            } else {
              this.$message.error(res.message)
            }
          })
          .finally(_ => {
            this.deleteLoading = false
          })
      } else {
        this.$message.error('数据异常，无法操作')
      }
    },
    publishReport(status) {
      this.publishLoading = true
      postAction('/patient/report/summary/update', {
        status: status,
        summary: this.report.summary,
        reportId: this.report.dataId
      })
        .then(res => {
          if (res.success) {
            this.$message.success(res.message)
            this.getMyFinalReport()
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(_ => {
          this.publishLoading = false
        })
    },
    physicalRecord() {
      console.log('father被触发了')
      const patientId = this.$route.query[PAGE_MENU_TYPE.PatientId]
      let query = {}
      query[PAGE_MENU_TYPE.ParamsField] = PAGE_MENU_TYPE.HealthManage
      query[PAGE_MENU_TYPE.PatientId] = patientId
      query.is_end_service = 1
      this.$router.push({
        path: '/health_record/physical',
        query
      })
    },
    laboratoryRecord() {
      console.log('father被触发了')
      const patientId = this.$route.query[PAGE_MENU_TYPE.PatientId]
      let query = {}
      query[PAGE_MENU_TYPE.ParamsField] = PAGE_MENU_TYPE.HealthManage
      query[PAGE_MENU_TYPE.PatientId] = patientId
      query.is_end_service = 1
      this.$router.push({
        path: '/health_record/laboratory',
        query
      })
    },
    icongraphyRecord() {
      console.log('father被触发了')
      const patientId = this.$route.query[PAGE_MENU_TYPE.PatientId]
      let query = {}
      query[PAGE_MENU_TYPE.ParamsField] = PAGE_MENU_TYPE.HealthManage
      query[PAGE_MENU_TYPE.PatientId] = patientId
      query.is_end_service = 1
      this.$router.push({
        path: '/health_record/iconography',
        query
      })
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .ant-descriptions-title {
  margin-bottom: 1px;
  background: rgb(245, 34, 45);
  color: rgb(255, 255, 255);
  padding: 20px;
}
/deep/ .ant-descriptions-item-label {
  width: 25%;
}
/deep/ .ant-descriptions-item-content {
  width: 25%;
}
</style>

